<?php
namespace Model;

use Model\Factory\TableGatewayFactory;

class MenuItem extends AbstractModel
{
    public $id;
    public $label;
    public $url;
    public $created_date;
    public $created_by;
    public $active;
    public $menu_id;
    public $lft;
    public $rgt;
    public $target;
    public $modifiable;

    public function getDirectParent(){
        $menuItemTable = TableGatewayFactory::getMenuItemTable();
        $select = $menuItemTable->getDbSqlSelect();
        $select->where(array('lft < '.$this->lft))
            ->where(array('rgt > '.$this->rgt))
            ->where(array('menu_id'=>$this->menu_id));
        $ancestorsRows = $menuItemTable->getTableGateway()->selectWith($select);
        if($ancestorsRows->count() == 0 )
            return null;
        $minLft = 0;
        $result = null;
        foreach ($ancestorsRows as $ancestor) {
            if($ancestor->lft > $minLft){
                $minLft = $ancestor->lft;
                $result = $ancestor;
            }
        }

        return $result;

    }

    public function getAllAncestors(){
        $menuItemTable = TableGatewayFactory::getMenuItemTable();
        $select = $menuItemTable->getDbSqlSelect();

        $select->where(array('lft < '.$this->lft))
            ->where(array('rgt > '.$this->rgt))
            ->where(array('menu_id'=>$this->menu_id));

        return $menuItemTable->getTableGateway()->selectWith($select);
    }


    public function getAllChildren(){
        $menuItemTable = TableGatewayFactory::getMenuItemTable();
        $select = $menuItemTable->getDbSqlSelect();

        $select->where(array('lft > '.$this->lft))
            ->where(array('rgt < '.$this->rgt))
            ->where(array('menu_id'=>$this->menu_id));

        return $menuItemTable->getTableGateway()->selectWith($select);
    }


    public function upMyPosition(){
        $menuItemTable = TableGatewayFactory::getMenuItemTable();
        $upperBrother = $menuItemTable->getTableGateway()->select(array(
            'rgt'=>($this->lft - 1), 'menu_id'=>$this->menu_id))->current();

        $leftDelta = $this->lft - $upperBrother->lft;
        $rightDelta =  $this->rgt - $upperBrother->rgt;
        $myChildren = $this->getAllChildren();
        $upperBrotherTree = $upperBrother->getAllChildren();
        foreach ($myChildren as $item) {
            $menuItemTable->update(array('lft'=>($item->lft - $leftDelta),
           "rgt"=>($item->rgt - $leftDelta) ), $item->id);
        }

        $menuItemTable->update(array('lft'=>($this->lft - $leftDelta),
            "rgt"=>($this->rgt - $leftDelta) ), $this->id);

        foreach ($upperBrotherTree as $item) {
            $menuItemTable->update(array('lft'=>($item->lft + $rightDelta),
                "rgt"=>($item->rgt + $rightDelta) ), $item->id);
        }

        $menuItemTable->update(array('lft'=>($upperBrother->lft + $rightDelta),
            "rgt"=>($upperBrother->rgt + $rightDelta) ), $upperBrother->id);

    }

    public function downMyPosition(){

        $menuItemTable = TableGatewayFactory::getMenuItemTable();
        $lowerBrother = $menuItemTable->getTableGateway()->select(array(
            'lft'=>($this->rgt + 1), 'menu_id'=>$this->menu_id))->current();
        $lowerBrother->upMyPosition();
    }



}
